﻿package frEngine.shader.filters.fragmentFilters
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import frEngine.manager.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;

    public class SimpleWaterNormalFilter extends FilterBase
    {
        public var waterMoveffset:Vector.<Number>;
        private var fcCommon:Vector.<Number>;
        private var fcWaterCommon:Vector.<Number>;
        private var fcWaterMove:Vector.<Number>;
        private var fcSpecularColor:Vector.<Number>;
        private var fcLightDir:Vector.<Number>;
        private var fcLightDiffuseColor:Vector.<Number>;
        private var fcLightSpecularColor:Vector.<Number>;
        private var fcAmbientColor:Vector.<Number>;
        private var fcEnvmapAlpha:Vector.<Number>;
        public var waterMap1:Texture3D;
        public var waterMap2:Texture3D;
        private var bgTex:Texture3D;
        public var cubeTex:Texture3D;
        private var defautltTangent:Vector.<Number>;
        public var UseDefaultTangent:Boolean = true;

        public function SimpleWaterNormalFilter(param1:Texture3D, param2:Texture3D, param3:Texture3D, param4:Texture3D)
        {
            this.waterMoveffset = this.Vector.<Number>([0.005, 0.007, 0.003, 0.004]);
            this.fcCommon = this.Vector.<Number>([0.5, 0, 1 / 255, 1]);
            this.fcWaterCommon = this.Vector.<Number>([0.5, 0, 0, 1]);
            this.fcWaterMove = this.Vector.<Number>([0, 0, 0, 0]);
            this.fcSpecularColor = this.Vector.<Number>([1, 1, 1, 100]);
            this.fcLightDir = this.Vector.<Number>([0.0597853, 0.0597853, 0.996419, 1]);
            this.fcLightDiffuseColor = this.Vector.<Number>([1, 0.992157, 0.772549, 1]);
            this.fcLightSpecularColor = this.Vector.<Number>([1, 0.992157, 0.772549, 1]);
            this.fcAmbientColor = this.Vector.<Number>([0.8, 0.4, 0.4, 1]);
            this.fcEnvmapAlpha = this.Vector.<Number>([1, 0, 0, 0]);
            this.defautltTangent = this.Vector.<Number>([1, 0, 0, 0]);
            this.bgTex = param1;
            this.waterMap1 = param2;
            this.waterMap2 = param3;
            this.cubeTex = param4;
            super(FilterType.SimpleWaterNormalFilter, FilterPriority.TangentNormalMapFilter);
            return;
        }// end function

        override public function setRegisterParams(param1:Program3dRegisterInstance) : void
        {
            param1.getParamByName("{fcCommon}", false).value = this.fcCommon;
            param1.getParamByName("{fcWaterCommon}", false).value = this.fcWaterCommon;
            param1.getParamByName("{fcWaterMove}", false).value = this.fcWaterMove;
            param1.getParamByName("{fcSpecularColor}", false).value = this.fcSpecularColor;
            param1.getParamByName("{fcLightDir}", false).value = this.fcLightDir;
            param1.getParamByName("{fcLightDiffuseColor}", false).value = this.fcLightDiffuseColor;
            param1.getParamByName("{fcLightSpecularColor}", false).value = this.fcLightSpecularColor;
            param1.getParamByName("{fcAmbientColor}", false).value = this.fcAmbientColor;
            param1.getParamByName("{fcEnvmapAlpha}", false).value = this.fcEnvmapAlpha;
            return;
        }// end function

        override public function dispose() : void
        {
            super.dispose();
            if (this.waterMap1)
            {
                Resource3dManager.instance.disposeTexture3dImp(this.waterMap1);
                this.waterMap1 = null;
            }
            if (this.waterMap2)
            {
                Resource3dManager.instance.disposeTexture3dImp(this.waterMap2);
                this.waterMap2 = null;
            }
            if (this.cubeTex)
            {
                Resource3dManager.instance.disposeTexture3dImp(this.cubeTex);
                this.cubeTex = null;
            }
            return;
        }// end function

        override public function get programeId() : String
        {
            return "SimpleWaterNormal0";
        }// end function

        override public function clone() : FilterBase
        {
            return this;
        }// end function

        override public function createVertexCode(param1:Program3dRegisterInstance, param2:int) : String
        {
            var _loc_3:* = new Array();
            _loc_3.push(new ToBuilderInfo("vf1", "{V_GloblePos}", false, 1));
            if (!this.UseDefaultTangent)
            {
                _loc_3.push(new ToBuilderInfo("vf2", "{TANGENT}", false, 1));
            }
            param1.toBuild("", _loc_3);
            return "";
        }// end function

        override public function createFragmentColor(param1:Program3dRegisterInstance, param2:Boolean) : String
        {
            var _loc_3:* = new Array();
            var _loc_4:String = "{TANGENT}";
            if (this.UseDefaultTangent)
            {
                _loc_3.push(new ToBuilderInfo("fc10", "{MyTangent}", false, 1, this.defautltTangent));
                _loc_4 = "{MyTangent}";
            }
            var _loc_5:String = "";
            _loc_3.push(new ToBuilderInfo("fs0", "{waveMap0}", false, 1, this.waterMap1));
            _loc_3.push(new ToBuilderInfo("fs1", "{waveMap1}", false, 1, this.waterMap2));
            _loc_3.push(new ToBuilderInfo("fs2", "{bg}", false, 1, this.bgTex));
            if (this.cubeTex)
            {
                _loc_3.push(new ToBuilderInfo("fs3", "{envMap}", false, 1, this.cubeTex));
                _loc_5 = EnvMapFilter.compileEnvMapFragmentCode("ft0", "ft1", "{fcCommon}", "{fcEnvmapAlpha}", "fs3", "ft4", "ft5", "ft2");
            }
            _loc_3.push(new ToBuilderInfo("fc0", "{fcCommon}", false, 1, this.fcCommon));
            _loc_3.push(new ToBuilderInfo("fc1", "{fcWaterCommon}", false, 1, this.fcWaterCommon));
            _loc_3.push(new ToBuilderInfo("fc2", "{fcWaterMove}", false, 1, this.fcWaterMove));
            _loc_3.push(new ToBuilderInfo("fc3", "{fcSpecularColor}", false, 1, this.fcSpecularColor));
            _loc_3.push(new ToBuilderInfo("fc4", "{fcLightDir}", false, 1, this.fcLightDir));
            _loc_3.push(new ToBuilderInfo("fc5", "{fcLightDiffuseColor}", false, 1, this.fcLightDiffuseColor));
            _loc_3.push(new ToBuilderInfo("fc6", "{fcLightSpecularColor}", false, 1, this.fcLightSpecularColor));
            _loc_3.push(new ToBuilderInfo("fc7", "{fcAmbientColor}", false, 1, this.fcAmbientColor));
            _loc_3.push(new ToBuilderInfo("fc8", "{fcEnvmapAlpha}", false, 1, this.fcEnvmapAlpha));
            _loc_3.push(new ToBuilderInfo("fc9", "{CamPos}", false, 1, Global3D.scene.cpos));
            var _loc_6:* = TangentNormalMapFilter.compileTangentNormalMapFragmentCode("{vnormal}", _loc_4, "ft1", "ft2", "ft3", "ft4") + "add ft5, {F_UVPostion}, {fcWaterMove}.xyxy \n" + "tex ft4, ft5, fs0 <2d,linear,wrap> \n" + "add ft5, {F_UVPostion}, {fcWaterMove}.zwzw \n" + "tex ft5, ft5, fs1 <2d,linear,wrap> \n" + "add ft4, ft4, ft5 \n" + "mul ft4, ft4, {fcWaterCommon}.x \n" + "sub ft4.xyz, ft4.xyz, {fcWaterCommon}.xxx\t \n" + "nrm ft4.xyz, ft4.xyz \n" + "m33 ft0.xyz, ft4, ft1 \n" + "mov ft0.w,   {fcWaterCommon}.y \n" + "sub ft1, {CamPos}, {V_GloblePos} \n" + "nrm ft1.xyz, ft1 \n" + "mov ft1.w,   ft1.w \n" + "dp3 ft3.x, {fcLightDir}, ft0 \n" + "max ft3.w, ft3.x, {fcCommon}.y \n" + "mul ft3.w, ft3.w, {fcLightDir}.w \n" + "mul ft3, ft3.w, {fcLightDiffuseColor} \n" + "add ft4, {fcLightDir}, ft1 \n" + "nrm ft4.xyz, ft4 \n" + "dp3 ft4.w, ft0, ft4 \n" + "sat ft4.w, ft4.w \n" + "pow ft4.w, ft4.w, {fcSpecularColor}.w \n" + "mul ft4.w, ft4.w, {fcLightDir}.w \n" + "mul ft4.xyz, {fcLightSpecularColor}, ft4.w \n" + "mov ft2, {fcAmbientColor} \n" + "tex ft5, {F_UVPostion}, fs2 <2d,linear,wrap> \n" + "sat ft3, ft3 \n" + "add ft2.xyz, ft3, ft2 \n" + "mul ft2.xyz, ft5, ft2 \n" + "mov ft2.w, ft5.w \n" + "add ft2.w, ft2.w, {fcCommon}.z \n" + "div ft2.xyz, ft2, ft2.w \n" + "sub ft2.w, ft2.w, {fcCommon}.z \n" + "sat ft2.xyz, ft2 \n" + "mul ft4.xyz, ft4, {fcSpecularColor} \n" + "add ft2.xyz, ft2, ft4 \n" + _loc_5 + "mov {output}, ft2 \n" + "";
            _loc_6 = param1.toBuild(_loc_6, _loc_3);
            return _loc_6;
        }// end function

        private function WaterMoveOffectset(param1:Number, param2:Number, param3:Number, param4:Number) : void
        {
            this.fcWaterMove[0] = this.fcWaterMove[0] + param1;
            this.fcWaterMove[1] = this.fcWaterMove[1] + param2;
            this.fcWaterMove[2] = this.fcWaterMove[2] + param3;
            this.fcWaterMove[3] = this.fcWaterMove[3] + param4;
            return;
        }// end function

        override public function update() : void
        {
            this.WaterMoveOffectset(this.waterMoveffset[0], this.waterMoveffset[1], this.waterMoveffset[2], this.waterMoveffset[3]);
            return;
        }// end function

    }
}
